package ex404.manager;

import java.util.ArrayList;
import java.util.List;

public class ProductInventory {
    private List<Product> productList;

    public ProductInventory() {
        productList = new ArrayList<>();
    }

    public Product getProduct(String id) {
        Product result = null;
        for (Product p : productList) {
            if (p.getId().equals(id)) {
                result = p;
                break;
            }
        }
        return result;
    }

    public void addProduct(Product product) {
        productList.add(product);
    }

    public boolean removeProduct(Product product) {
        if (product.getQuantity() > 0) {
            return false;
        } else {
            productList.remove(product);
            return true;
        }
    }

    public int getTotalQuantity() {
        int result = 0;
        for (Product p : productList) {
            result += p.getQuantity();
        }
        return result;
    }

    public double getTotalAmount() {
        double result = 0.0;
        for (Product p : productList) {
            result += p.getQuantity() * p.getPrice();
        }
        return result;
    }

    public Product[] toArray() {
        return productList.toArray(new Product[0]);
    }
}
